package org.jeecg.modules.qyxx.service.impl;

import cn.hutool.system.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.Rest;
import org.jeecg.modules.qyxx.entity.req.NsrsbhShxydmRequst;
import org.jeecg.modules.qyxx.entity.resp.BaseResponse;
import org.jeecg.modules.qyxx.mapper.CompanyInfoMapper;
import org.jeecg.modules.qyxx.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LoginServiceImpl implements ILoginService {
    @Override
    public Rest<BaseResponse> loginByNsrsbhOrShxydm(NsrsbhShxydmRequst req) throws Exception {
        return null;
    }

    /*@Autowired
    private NsrService nsrService;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private CompanyInfoMapper companyInfoMapper;

    @Autowired
    private SwyAPIService swyAPIService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Value("${foresee.gryhmmkey.key}")
    private String gryhmmkey; //个人用户密码加密key
    @Override
    public Rest<BaseResponse> loginByNsrsbhOrShxydm(NsrsbhShxydmRequst req) throws Exception {
        String nsrsbh = req.getNsrsbh() != null ? req.getNsrsbh() : req.getShxydm();
        BaseResponse response = new BaseResponse();
        Object sessionId = RedisUtil.get(RedisKeyConstant.redisKfptSessionKey + nsrsbh); //redis里取sessionId
        Object areaCode = RedisUtil.get(RedisKeyConstant.redisKfptAreaCodeKey + nsrsbh); //redis里取areacode
        Object gdslxDm = RedisUtil.get(RedisKeyConstant.redisKfptGdslxdmKey + nsrsbh); //redis里取gdslxdm
        long expire = RedisUtil.getExpire(RedisKeyConstant.redisKfptSessionKey + nsrsbh); //redis里sessionId的剩余生存时间
        if ( expire >=0 && expire <= 60){
            swyAPIService.keepSessionByNsrsbh(nsrsbh);
        }
        if (sessionId != null && areaCode != null && gdslxDm != null) {
            return new Rest<>(response);
        } else {
            List<UserInfo> userInfoList = this.getUserInfoByNsrsbh(nsrsbh); //读数据库获得电局用户登录信息
            if (userInfoList == null || userInfoList.size() == 0) {
                response.setCode("-1");
                response.setMsg("授权登录纳税人识别号传入错误，无法识别，请校正后重试");
                return new Rest<>(response);
            }
            String redisErrorResponse = (String) RedisUtil.get(RedisKeyConstant.redisLoginErrorNsrsbhKey + nsrsbh);
            if (StringUtils.isNotBlank(redisErrorResponse)) {
                response = JSONObject.parseObject(redisErrorResponse,BaseResponse.class);
                return new Rest<>(response);
            }
            UserInfo userInfo = new UserInfo();
            List<String> errorMsgList = new ArrayList<>();
            //判断最新登录是否有解绑该纳税人识别号
            AppNsrLoginResponse loginResponse = this.getRealLoginResponseInDataSourceByNsrsbh(errorMsgList, userInfoList, nsrsbh, userInfo , null);
            if (!BszsResultEnum.SUCCESS_OLD.getCode().equals(loginResponse.getCode()) && (loginResponse.getMsg()).indexOf("密码") != -1) {
                response.setCode(ApiExceptionEnum.NOT_CORRECT_PWD.getCode());
                response.setMsg(JSON.toJSONString(errorMsgList));
                RedisUtil.set(RedisKeyConstant.redisLoginErrorNsrsbhKey + nsrsbh,JSON.toJSONString(response),3600L);
                return new Rest<>(response);
            }
            if (!BszsResultEnum.SUCCESS_OLD.getCode().equals(loginResponse.getCode())) {
                response.setCode(loginResponse.getCode());
                response.setMsg(JSON.toJSONString(errorMsgList));
                RedisUtil.set(RedisKeyConstant.redisLoginErrorNsrsbhKey + nsrsbh,JSON.toJSONString(response),3600L);
                return new Rest<>(response);
            }
            RedisUtil.set(RedisKeyConstant.redisKfptSessionKey + nsrsbh, loginResponse.getSessionId(), RedisKeyConstant.kfptSessionIdLifeTime);
            RedisUtil.set(RedisKeyConstant.redisKfptAreaCodeKey + nsrsbh, userInfo.getSwyRegionCode(), RedisKeyConstant.kfptSessionIdLifeTime);
            RedisUtil.set(RedisKeyConstant.redisKfptGdslxdmKey + nsrsbh, userInfo.getLoginGdslxdm(), RedisKeyConstant.kfptSessionIdLifeTime);
            RedisUtil.set(RedisKeyConstant.redisKfptClientNoKey + nsrsbh, loginResponse.getClientNo(), RedisKeyConstant.kfptSessionIdLifeTime);
            response.setCode("0");
            response.setMsg("成功");
            return new Rest<>(response);
        }*/
    }